clc
% Basic Configuration (Users define)
tablePath = 'workitems.xlsx';
modelPath = 'SetCndt.slx';
testPath = 'QualificationTest';
casePath = 'D:\Matlab_File\Model\TestCases\';

% Load table and model
table = readtable(tablePath);
load_system(modelPath);
open_system(modelPath);

% Variable Configuration
idIndex = 1;
titleIndex = 2;
typeIndex = 3;
firstPoint = 1;
tableRows = height(table);
tableColums = width(table);

% Create test harness and test file
if (strcmp(testType, 'QualificationTest'))
    disp('Create Qualification Test Harness for model');
    harnessName = append(erase(modelPath, '.slx'), '_QualificationTestHarness');
    testFileName = append(erase(modelPath, '.slx'), '_QualificationTest');
else if (strcmp(testType, 'IntegrationTest'))
    disp('Create Integration Test Harness for model');
    harnessName = append(erase(modelPath, '.slx'), '_IntegrationTestHarness');
    testFileName = append(erase(modelPath, '.slx'), '_IntegrationTest');
else
    disp('Unknow test type!');
end

sltest.harness.create(bdroot, 'Name', harnessName);
sltest.harness.open(bdroot, harnessName);
tf = sltest.testmanager.TestFile(testFileName);

% Remove the default test suite
tsDel = getTestSuiteByName(tf, 'New Test Suit 1 ');
remove(tsDel);

% Test coverage setting
cov = getCoverageSettings(tf);
cov.RecordCoverage = true;
cov.MetricSettings = 'dmc'; % Decision Condition MCDC

% Create Test Suits and Cases
while firstPoint <= tableRows
    last Point = firstPoint + 1;
    if lastPoint > tableRows
        if strcmp(table(firstPoint,typeIndex).Type{1}, 'Software Qualification Test Case') && ...
           strcmp(table(firstPoint,titleIndex).Title{1}(1:5), 'UESTC') % filter out cases dont have 'UESTC'
            testCaseName = append(table(firstPoint,idIndex).ID{1}, '_');
            tc = createTestCase(ts, 'baseline', testCaseName);

            % Configure the attribute of test case
            tc.Tags = table(firstPoint,idIndex).ID{1};
            tc.Description = (firstPoint,titleIndex).Title{1};

            % Configure the harness of test case
            setProperty(tc, 'Model', erase(modelPath, '.slx'));
            setProperty(tc, 'HarnessOwner', erase(modelPath, '.slx'), 'HarnessName', harnessName);

            % Create input file and capture baseline file
            createInputDataFile(tc, append(folderPath, '\', table(firstPoint,idIndex).ID{1}, '_baseline.xlsx'), true);
            firstPoint = firstPoint + 1;
        else
            firstPoint = firstPoint + 1;
        end
    else
        if strcmp(table(firstPoint,typeIndex).Type{1}, 'Heading')
            if strcmp(table(lastPoint,typeIndex).Type{1}, 'Heading')
                firstPoint = lastPoint;
            elseif strcmp(table(lastPoint,typeIndex).Type{1}, 'Software Qualification Test Case')
                ts = createTestSuite(tf, table(firstPoint,titleIndex).Title{1});

                % Create a folder for cases
                folderPath = append(casePath, strrep(ts.Name, ' ', '_'));
                if ~isfolder(folderPath)
                    mkdir(folderPath)
                end
                firstPoint = firstPoint + 1;
            end
        elseif strcmp(table(firstPoint,typeIndex).Type{1}, 'Software Qualification Test Case') && ...
               strcmp(table(firstPoint,titleIndex).Title{1}(1:5), 'UESTC') % filter out cases dont have 'UESTC'
            testCaseName = append(table(firstPoint,idIndex).ID{1}, '_');
            tc = createTestCase(ts, 'baseline', testCaseName);

            % Configure the attribute of test case
            tc.Tags = table(firstPoint,idIndex).ID{1};
            tc.Description = (firstPoint,titleIndex).Title{1};

            % Configure the harness of test case
            setProperty(tc, 'Model', erase(modelPath, '.slx'));
            setProperty(tc, 'HarnessOwner', erase(modelPath, '.slx'), 'HarnessName', harnessName);

            % Create input file and capture baseline file
            createInputDataFile(tc, append(folderPath, '\', table(firstPoint,idIndex).ID{1}, '_baseline.xlsx'), true);
            firstPoint = firstPoint + 1;
        else
            firstPoint = firstPoint + 1;
        end
    end
end

disp('Successfully!');

